/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package implementation.client;

import implementation.client.net.ClientConnectionManager;
import implementation.shared.EPacketType;
import implementation.shared.IPacket;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.thoughtworks.xstream.XStream;

/**
 *
 * @author Administrator
 */
public class JSPServerStub extends ServerStub{

	private XStream xstream;
	
    public JSPServerStub() {
                this._connectionManager = new ClientConnectionManager();
		this._inbox = new ConcurrentLinkedQueue<IPacket>();
		this._notificationInbox = new ConcurrentLinkedQueue<IPacket>();
		this.xstream = new XStream();
		this._listener = new Thread(new Runnable() {
		
			public void run() {
				try {
					while (true) {
						IPacket packet;
						packet = _connectionManager.recieve();
						if (packet.getType() == EPacketType.ENotification) {
							_notificationInbox.add(packet);
							synchronized (_notificationInbox) {
								_notificationInbox.notify();
							}
						} else {
							_inbox.add(packet);
							synchronized (_inbox) {
								_inbox.notify();
							}
						}
					}
				} catch (InterruptedException e) {
				}
			}
		});
    }
    
    public String getForumsJSP() {
    	return xstream.toXML(super.getForums());
    }

    public String getThreadsJSP(int forumID) {
    	return xstream.toXML(super.getThreads(forumID));
    }
    
}
